import { Type } from '@angular/core';
import { NzModalService } from 'ng-zorro-antd/modal';
import { Observable } from 'rxjs';
import { RefEntityDetailComponent } from './ref-entity-detail.component';

export abstract class RefEntityDetailService<T> {
  protected defaultWidth: number = 600;
  constructor(
    protected entityName: string,
    protected componentClass: Type<RefEntityDetailComponent<T>>,
    protected modalService: NzModalService
  ) {}
  show(model?: T, options?: { title?: string; width?: number }): Observable<any> {
    const defaultTitle = !model ? `添加${this.entityName}` : `编辑${this.entityName}`;
    var nzTitle = !options?.title ? defaultTitle : options.title;
    var nzWidth = !options?.width ? this.defaultWidth : options.width;
    let modalRef = this.modalService.create({
      nzTitle: nzTitle,
      nzMaskClosable: false,
      nzKeyboard: true,
      nzWidth: nzWidth,
      nzComponentParams: {
        model: !model ? null : model,
      },
      nzContent: this.componentClass,
    });
    return modalRef.afterClose.asObservable();
  }
}
